//
//  ViewController.m
//  给静态图片添加滤镜
//
//  Created by 聚点互动iOS on 2020/12/18.
//

#import "ViewController.h"
#import <GPUImage/GPUImage.h>
#import <Photos/Photos.h>

@interface ViewController ()
{
    GPUImagePicture *_picture;
    GPUImageView *_gpuImageView;
    GPUImageFilter *_filter;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self step1];
    [self step2];
    [self step3];
    [self step4];
    [self step5];
    [self step6];
    
    [self addFuntionBtns];
    
}

/// 1. 创建GPUImagePicture
- (void)step1{
    UIImage *image = [UIImage imageNamed:@"demo_test"];
    GPUImagePicture *picture = [[GPUImagePicture alloc] initWithImage:image];
    _picture = picture;
}


/// 2. 创建GPUImageView，用于展示实时的图片效果
- (void)step2{
    GPUImageView *gpuImageView = [[GPUImageView alloc] init];
    gpuImageView.frame = self.view.bounds;
    [self.view addSubview:gpuImageView];
    _gpuImageView = gpuImageView;
}

/// 3. 创建滤镜
- (void)step3{
    GPUImageFilter *filter  = [[GPUImageFilter alloc] init];
    _filter = filter;
}

/// 4. 为GPUImagePicture添加滤镜
- (void)step4{
    [_picture addTarget:_filter];
}

/// 5. 为滤镜结果输出给GPUImageView
- (void)step5{
    [_filter addTarget:_gpuImageView];
}


/// 6. 处理图片
- (void)step6{
    [_picture processImage];
}

/// 获取滤镜处理过的图片
- (void)takeResultPhoto{
    
    {
        UIImage *newImage = [_filter imageFromCurrentFramebuffer];
        
        [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
    
            [[PHAssetCreationRequest creationRequestForAsset] addResourceWithType:PHAssetResourceTypePhoto data:newImage options:nil];
            
        } completionHandler:^(BOOL success, NSError * _Nullable error) {
        }];
    }
}

#pragma mark - ****
- (void)addFuntionBtns{
    NSArray<NSString *> *names = @[
        @"素描",
        @"灰色",
        @"原图",
        @"拍照",
    ];
    
    [names enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
         
        UIButton *btn = [UIButton new];
        [btn setTitle:obj forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor lightTextColor] forState:UIControlStateNormal];
        [self.view addSubview:btn];
        btn.tag = idx;
        btn.backgroundColor = [UIColor greenColor];
        btn.frame = CGRectMake(0, 100 + idx * 40, 100, 30);
        [btn addTarget:self action:@selector(btnDidClicked:) forControlEvents:UIControlEventTouchUpInside];
    }];
    
}

- (void)btnDidClicked:(UIButton *)sender{
    if (sender.tag == 0) {
        [self removeAllTarget];
        
        // 素描
        GPUImageSketchFilter *filter = [GPUImageSketchFilter new];
        _filter = filter;
        
        [self step4];
        [self step5];
        [self step6];
        
    }else if(sender.tag == 1){
        [self removeAllTarget];
        
        // 灰色
        GPUImageGrayscaleFilter *filter  = [[GPUImageGrayscaleFilter alloc] init];
        _filter = filter;
        
        [self step4];
        [self step5];
        [self step6];
        
    }else if(sender.tag == 2){
        [self removeAllTarget];
        
        
        GPUImageFilter *filter  = [[GPUImageFilter alloc] init];
        _filter = filter;
        
        [self step4];
        [self step5];
        [self step6];
        
    }else if(sender.tag == 3){
        [self takeResultPhoto];
    }
}

- (void)removeAllTarget{
    [_filter removeAllTargets];
    [_picture removeAllTargets];
}


@end
